package belf.migrate.api.taskconf;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.*;

import java.util.List;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TableSyncConf {
    @JsonProperty("table")
    private String tableName;

    @JsonProperty("syncType")
    private SyncType syncType;  // 全量 增量

    @JsonProperty("pageSize")
    private Integer pageSize;  // 每次读取的行数100，含大对象字段20、超1G大对象字段5

    @JsonProperty("slice")
    private Integer slice;  // 读取分片数

    @JsonProperty("sliceSize")
    private Integer sliceSize; // 每片500万行或5G数据，目前 total row count / slice

    @JsonProperty("readThreads")
    private Integer readThreads;  // 读线程数，目前 =slice

    @JsonProperty("writeThreads")
    private Integer writeThreads;  // 写线程数，暂时没有使用

    @JsonProperty("sortBy")
    private String sortBy;  // 增量同步的排序字段名称，primary key or unique identifier or datetime

    @JsonProperty("last")
    private Object last;  // 增量同步已完成的排序字段的位置（值）,下一次从大于不等于该值开始

    @Deprecated
    @JsonProperty("columns")
    private List<ColumnSyncConf> columns;  // 移至TaskConf.mapping


    public String toJson() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writer().writeValueAsString(this);
    }

    @Getter
    public enum SyncType {
        FULL_SYNC("FULL_SYNC", "全量同步"),
        INCREMENT_SYNC("INCREMENT_SYNC", "增量同步");

        private final String name;
        private final String desc;

        SyncType(String name, String desc) {
            this.name = name;
            this.desc = desc;
        }

    }
}
